package com.corgi.mybatis.modules.controller;

import cn.hutool.core.bean.BeanUtil;
import com.corgi.mybatis.modules.domain.Attachment;
import com.corgi.mybatis.modules.service.AttachmentService;
import com.corgi.mybatis.util.AesUtil;
import com.corgi.mybatis.util.RsaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @title: AttachmentController
 * @description:
 * @author: dengmiao
 * @create: 2019-10-26 12:19
 **/
@Slf4j
@RestController
@RequestMapping("attachment")
public class AttachmentController {

    private final static String AES_KEY = "1234567890123456";

    private final static String AES_IV = "1234567890000000";

    @Autowired
    private AttachmentService attachmentService;

    @PostMapping("save")
    public void save(@RequestParam Map<String, Object> param) throws IOException {
        Attachment attachment = new Attachment();
        BeanUtil.copyProperties(param, attachment);
        attachment.setFile(RsaUtil.base642Byte(AesUtil.decrypt(param.get("files").toString(), AES_KEY, AES_IV)));
        attachmentService.save(attachment);
    }

    @GetMapping("list")
    public List<Attachment> list() {
        return attachmentService.selectList();
    }
}
